﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using ECGadmin.Common;
using ECGadmin.DAL;
using ECGadmin.WebUI.Models;
using ECGadmin.WebUI.Attributes;

namespace ECGadmin.WebUI.Controllers
{
    [AdminLoggedAuthorize]
    public class ProductTagController : Controller
    {
        //
        // GET: /ProductTag/

        public ActionResult Index(int currentPage = 1)
        {
            var totalPage = 0;
            var totalRecord = 0;
            var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["DefaultItemsPerPage"]);

            var model = ProductTagDb.GetProductTagListPaging(currentPage, out totalPage, out totalRecord, itemsPerPage) ?? new List<ViewProductTagListDto>();

            ViewBag.PageInfo = new PageInfo { CurrentPage = currentPage, PageSize = itemsPerPage, TotalPages = totalPage };

            return View(model);
        }

        public ActionResult EditTagForm(int id)
        {
            var model = ProductTagDb.GetById(id) ?? new ProductTagDto();
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditTagForm(ProductTagDto model)
        {
            var result = ProductTagDb.UpdateProductTag(model);
            var data = new { Result = result > 0, model.Name, model.Id };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
